package ljl.alg.jianzhioffer.round2;

import commons.ListNode;

public class _19_delete_node_in_link_list {
    
    /*
    * 舒服了...
    * 我就适合这种难度的弱智脑残低能题！
    *
    * 居然写错了。。
    * 怎么这么怪呢？
    * */
    public ListNode deleteNode2(ListNode head, int val) {
        if (head.val == val) return head.next;
        ListNode p = head;
        ListNode prev = null;
        while (head != null) {
            if (head.val == val) {
                prev.next = head.next;
                break;
            }
            prev = head;
            head = head.next;
        }
        return p;
    }
    
    // 还是得看路飞
    public ListNode deleteNode(ListNode head, int val) {
        if (head.val == val) return head.next;
        ListNode cur = head.next, prev = head;
        while (cur != null && cur.val != val) {
            prev = cur;
            cur = cur.next;
        }
        if (cur != null) prev.next = cur.next;
        return head;
    }
}
